﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TN_CMS.Core.Abstract;
using TN_CMS.Core.Entities;

namespace TN_CMS.Core.Concrete
{
    public class EFSysAdminRepository : ISysAdminRepository
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<SysAdmin> SysAdmins
        {
            get { return context.SysAdmins; }
        }

        public IQueryable<SysAdmin> GetList(out int total, int pageIndex = 0, int pageSize = 10, bool getInactive = false)
        {
            var entries = SysAdmins
                .Where(a => a.IsActive == true || a.IsActive == !getInactive)
                .OrderBy(a => a.Id)
                .Skip((pageIndex - 1) * pageSize)
                .Take(pageSize);
            total = SysAdmins
                .Count(a => a.IsActive == true || a.IsActive == !getInactive);
            return entries;
        }

        public SysAdmin GetDetail(int id, bool getInactive = false)
        {
            return SysAdmins.FirstOrDefault(a => a.Id == id && a.IsActive == true || a.IsActive == !getInactive);
        }

        public void Save(SysAdmin admin, int actorId)
        {
            bool needToSave = false;
            if (admin.Id == 0)
            {
                admin.CreatedBy = actorId;
                admin.CreatedOn = DateTime.Now;
                context.SysAdmins.Add(admin);
            }
            else
            {
                SysAdmin dbEntry = GetDetail(admin.Id);
                if (dbEntry != null)
                {
                    dbEntry.Name = admin.Name;
                    dbEntry.Email = admin.Email;
                    dbEntry.UpdatedOn = DateTime.Now;
                    dbEntry.UpdatedBy = actorId;
                    dbEntry.IsActive = admin.IsActive;
                    needToSave = true;
                }
            }
            if (needToSave)
            {
            context.SaveChanges();
        }
        }

        public void ChangeActiveStatus(int[] arrId, bool isActive)
        {
            bool needToSave = false;
            foreach (var id in arrId)
            {
                SysAdmin dbEntry = GetDetail(id);
                if (dbEntry != null)
                {
                    dbEntry.IsActive = isActive;
                    needToSave = true;
                }
            }
            if (needToSave)
            {
                context.SaveChanges();
            }
        }

        public void Delete(int[] arrId)
        {
            bool needSaveChange = false;
            foreach (var id in arrId)
            {
                SysAdmin dbEntry = GetDetail(id);
                if (dbEntry != null)
                {
                    context.SysAdmins.Remove(dbEntry);
                    needSaveChange = true;
                }
            }
            if (needSaveChange)
            {
                context.SaveChanges();
            }
        }
    }
}
